package com.zimmur.app.mocolink.manage.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zimmur.app.mocolink.domain.model.GoodsBrand;
import com.zimmur.app.mocolink.domain.param.GoodsBrandReqParam;
import com.zimmur.app.mocolink.domain.util.PageUtil;
import com.zimmur.app.mocolink.domain.vo.GoodsBrandInfo;
import com.zimmur.app.mocolink.manage.web.shiro.token.TokenManager;
import com.zimmur.app.mocolink.service.IGoodsBrandService;

@Controller
@RequestMapping("/goodsBrand")
public class GoodsBrandController extends BaseController{
	private Logger log= LoggerFactory.getLogger(GoodsBrandController.class);
	@Resource
	private IGoodsBrandService brandService;
	
	@RequestMapping("/index")
	public  String index(Model model){
		GoodsBrandReqParam brandParam = new GoodsBrandReqParam();
		brandParam.setPageNo(1);
		//1.查询品牌信息
		PageUtil<GoodsBrand> page = brandService.queryBrandInfo(brandParam);
		model.addAttribute("param", brandParam);
		model.addAttribute("page", page);
		return "/center/goodsBrand/list";
	}
	/**
	 * 综合检索商品品牌信息
	 * @return
	 */
	@RequestMapping("/searchGoodsBrand")
	public  String searchGoods(Model model){
		Integer pageNo=getParam("pageNo", 1);
		Integer pageSize=getParam("pageSize", 10);
		String keyword=getParam("keyword");
		GoodsBrandReqParam brandParam = new GoodsBrandReqParam();
		brandParam.setKeyword(keyword);
		brandParam.setPageNo(pageNo);
		brandParam.setPageSize(pageSize);
		//1.查询品牌信息
		PageUtil<GoodsBrand> page = brandService.queryBrandInfo(brandParam);
		model.addAttribute("param", brandParam);
		model.addAttribute("page", page);
		return "/center/goodsBrand/list";
	}
	/**
	* <p>Title: edit</p>
	* <p>Description: 初始化编辑页面</p>
	* @param model
	* @return
	 */
	@RequestMapping("/edit")
	public  String edit(Model model){
		Integer brandId  = getParam("brandId",0);
		//1.查询品牌信息
		GoodsBrand vo =null;
		if(brandId == 0){//新增初始化编辑页面
		    vo = new GoodsBrand();
		}else{//修改商品信息初始化编辑页面
			vo = brandService.queryGoodsBrand(brandId);
		}
		model.addAttribute("goodsBrand", vo);
		return "/center/goodsBrand/edit";
	}
	/**
	* <p>Title: saveGoodsBrand</p>
	* <p>Description:保存商品品牌信息 </p>
	* @param model
	* @return
	 */
	@ResponseBody
	@RequestMapping("/saveGoodsBrand")
	public  Map<String,String> saveGoodsBrand(GoodsBrandInfo brand){
		Map<String,String> map=new HashMap<String, String>();
		String userName = TokenManager.getNickname(); 
	    int flag=0;
	    Integer brandId = brand.getId();
	    if(brandId!=null&&brandId>0){//修改商品信息保存数据
	    	flag = brandService.updateGoodsBrand(brand);
	    }else{//新增商品信息保存数据
	    	flag = brandService.saveGoodsBrand(brand,userName);
	    }
		if(flag>0){
			map.put("code","000000");
			map.put("msg", "操作成功");
		}else{
			map.put("code","100000");
			map.put("msg", "操作失败");
		}
		return map;
	}
	/**
	* <p>Title: deleteGoodsBrand</p>
	* <p>Description: 删除商品品牌信息</p>
	* @param model
	* @return
	 */
	@ResponseBody
	@RequestMapping("/deleteGoodsBrand")
	public  Map<String,String> deleteGoodsBrand(Model model){
		Map<String,String> map=new HashMap<String, String>();
		Integer brandId= getParam("brandId", 0);
		Integer flag = brandService.deleteGoodsBrand(brandId);
		if(flag>0){
			map.put("code","000000");
			map.put("msg", "操作成功");
		}else{
			map.put("code","100000");
			map.put("msg", "操作失败");
		}
		return map;
	}
}
